{% extends 'base.html' %}
{% load static %}

{% block title %}耗材管理 - 固定资产管理系统{% endblock %}

{% block content %}
<div class="row">
    <div class="col-md-12">
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="{% url 'assets:index' %}">首页</a></li>
                <li class="breadcrumb-item active" aria-current="page">耗材管理</li>
            </ol>
        </nav>
        
        <div class="d-flex justify-content-between align-items-center mb-3">
            <h2><i class="fas fa-cubes me-2"></i>耗材管理</h2>
            <div class="btn-group">
                <a href="{% url 'assets:consumable_create' %}" class="btn btn-primary">
                    <i class="fas fa-plus me-1"></i>新增耗材
                </a>
                <button type="button" class="btn btn-outline-primary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown">
                    <span class="visually-hidden">更多操作</span>
                </button>
                <ul class="dropdown-menu">
                    <li><a class="dropdown-item" href="{% url 'assets:bulk_import_consumables' %}">
                        <i class="fas fa-upload me-2"></i>批量导入
                    </a></li>
                    <li><hr class="dropdown-divider"></li>
                    <li><a class="dropdown-item text-warning" href="#" onclick="batchUpdateStock()">
                        <i class="fas fa-edit me-2"></i>批量更新库存
                    </a></li>
                    <li><a class="dropdown-item text-danger" href="#" onclick="batchDeleteConsumables()">
                        <i class="fas fa-trash me-2"></i>批量删除
                    </a></li>
                </ul>
            </div>
        </div>
        
        <!-- 搜索和筛选 -->
        <div class="card mb-4">
            <div class="card-body">
                <form method="get" class="row g-3">
                    <div class="col-md-4">
                        <div class="input-group">
                            <input type="text" class="form-control" name="search" placeholder="搜索耗材名称或编码..." value="{{ search_query }}">
                            <button class="btn btn-outline-secondary" type="submit">
                                <i class="fas fa-search me-1"></i>搜索
                            </button>
                        </div>
                    </div>
                    <div class="col-md-2">
                        <select class="form-select" name="status" onchange="this.form.submit()">
                            <option value="">全部状态</option>
                            <option value="low_stock" {% if request.GET.status == 'low_stock' %}selected{% endif %}>库存不足</option>
                            <option value="normal" {% if request.GET.status == 'normal' %}selected{% endif %}>库存正常</option>
                            <option value="out_of_stock" {% if request.GET.status == 'out_of_stock' %}selected{% endif %}>缺货</option>
                        </select>
                    </div>
                </form>
            </div>
        </div>    
    <!-- 耗材列表 -->
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h5 class="mb-0">
                    <i class="fas fa-list me-2"></i>耗材列表
                    {% if page_obj %}
                        <span class="badge bg-secondary ms-2">{{ page_obj.paginator.count }} 个耗材</span>
                    {% endif %}
                </h5>
                <div class="form-check">
                    <input class="form-check-input" type="checkbox" id="selectAll" onchange="toggleSelectAll()">
                    <label class="form-check-label" for="selectAll">全选</label>
                </div>
            </div>
            <div class="card-body">
                <form id="batchActionForm" method="post">
                    {% csrf_token %}
                    <div class="table-responsive">
                        <table class="table table-striped table-hover">
                            <thead class="table-dark">
                                <tr>
                                    <th width="5%">
                                        <input type="checkbox" class="form-check-input" id="selectAllHeader" onchange="toggleSelectAll()">
                                    </th>
                                    <th width="15%">耗材名称</th>
                                    <th width="12%">耗材编码</th>
                                    <th width="10%">类别</th>
                                    <th width="10%">仓库</th>
                                    <th width="8%">单位</th>
                                    <th width="8%">当前库存</th>
                                    <th width="8%">最低库存</th>
                                    <th width="10%">单价</th>
                                    <th width="14%">操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for consumable in page_obj %}
                                <tr>
                                    <td>
                                        <input type="checkbox" class="form-check-input consumable-checkbox" name="consumable_ids" value="{{ consumable.pk }}">
                                    </td>
                                    <td>
                                        <div class="d-flex align-items-center">
                                            <i class="fas fa-cube me-2 text-success"></i>
                                            <div>
                                                <strong>{{ consumable.name }}</strong>
                                                {% if consumable.description %}
                                                    <br><small class="text-muted">{{ consumable.description|truncatechars:30 }}</small>
                                                {% endif %}
                                            </div>
                                        </div>
                                    </td>
                                    <td>
                                        <span class="badge bg-info">{{ consumable.code|default:"-" }}</span>
                                    </td>
                                    <td>
                                        {% if consumable.category %}
                                            <span class="badge bg-secondary">{{ consumable.category }}</span>
                                        {% else %}
                                            <span class="text-muted">-</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        {% if consumable.warehouse %}
                                            {{ consumable.warehouse.name }}
                                        {% else %}
                                            <span class="text-muted">-</span>
                                        {% endif %}
                                    </td>
                                    <td>{{ consumable.unit|default:"-" }}</td>
                                    <td>
                                        {% if consumable.current_stock <= consumable.min_stock %}
                                            <span class="badge bg-danger">
                                                <i class="fas fa-exclamation-triangle me-1"></i>{{ consumable.current_stock }}
                                            </span>
                                        {% elif consumable.current_stock <= consumable.min_stock|add:"10" %}
                                            <span class="badge bg-warning">
                                                <i class="fas fa-exclamation me-1"></i>{{ consumable.current_stock }}
                                            </span>
                                        {% else %}
                                            <span class="badge bg-success">{{ consumable.current_stock }}</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        <span class="text-muted">{{ consumable.min_stock|default:"-" }}</span>
                                    </td>
                                    <td>
                                        {% if consumable.unit_price %}
                                            <span class="text-success fw-bold">¥{{ consumable.unit_price|floatformat:2 }}</span>
                                        {% else %}
                                            <span class="text-muted">-</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        <div class="btn-group btn-group-sm">
                                            <a href="{% url 'assets:consumable_update' consumable.pk %}" class="btn btn-outline-primary" title="编辑">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                            <a href="{% url 'assets:consumable_delete' consumable.pk %}" class="btn btn-outline-danger" title="删除" onclick="return confirm('确定要删除耗材 {{ consumable.name }} 吗？')">
                                                <i class="fas fa-trash"></i>
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                                {% empty %}
                                <tr>
                                    <td colspan="10" class="text-center py-4">
                                        <div class="text-muted">
                                            <i class="fas fa-cubes fa-2x mb-2"></i>
                                            <p class="mb-0">暂无耗材数据</p>
                                        </div>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </form>
            </div>
        </div>

        <!-- 分页 -->
        {% if page_obj.has_other_pages %}
        <nav aria-label="页面导航" class="mt-4">
            <ul class="pagination justify-content-center">
                {% if page_obj.has_previous %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if search_query %}&search={{ search_query }}{% endif %}{% if request.GET.status %}&status={{ request.GET.status }}{% endif %}">
                            <i class="fas fa-chevron-left"></i>
                        </a>
                    </li>
                {% endif %}
                
                {% for num in page_obj.paginator.page_range %}
                    {% if page_obj.number == num %}
                        <li class="page-item active">
                            <span class="page-link">{{ num }}</span>
                        </li>
                    {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                        <li class="page-item">
                            <a class="page-link" href="?page={{ num }}{% if search_query %}&search={{ search_query }}{% endif %}{% if request.GET.status %}&status={{ request.GET.status }}{% endif %}">{{ num }}</a>
                        </li>
                    {% endif %}
                {% endfor %}
                
                {% if page_obj.has_next %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.next_page_number }}{% if search_query %}&search={{ search_query }}{% endif %}{% if request.GET.status %}&status={{ request.GET.status }}{% endif %}">
                            <i class="fas fa-chevron-right"></i>
                        </a>
                    </li>
                {% endif %}
            </ul>
        </nav>
        {% endif %}
    </div>
</div>

<script>
function toggleSelectAll() {
    const selectAllCheckbox = document.getElementById('selectAll') || document.getElementById('selectAllHeader');
    const consumableCheckboxes = document.querySelectorAll('.consumable-checkbox');
    const isChecked = selectAllCheckbox.checked;
    
    consumableCheckboxes.forEach(checkbox => {
        checkbox.checked = isChecked;
    });
    
    // 同步两个全选复选框的状态
    const selectAll = document.getElementById('selectAll');
    const selectAllHeader = document.getElementById('selectAllHeader');
    if (selectAll) selectAll.checked = isChecked;
    if (selectAllHeader) selectAllHeader.checked = isChecked;
}

function batchDeleteConsumables() {
    const checkedBoxes = document.querySelectorAll('.consumable-checkbox:checked');
    
    if (checkedBoxes.length === 0) {
        alert('请选择要删除的耗材');
        return;
    }
    
    if (confirm(`确定要删除选中的 ${checkedBoxes.length} 个耗材吗？\n\n此操作不可撤销！`)) {
        const form = document.getElementById('batchActionForm');
        form.action = '{% url "assets:consumable_batch_delete" %}';
        form.submit();
    }
}

function batchUpdateStock() {
    const checkedBoxes = document.querySelectorAll('.consumable-checkbox:checked');
    
    if (checkedBoxes.length === 0) {
        alert('请选择要更新库存的耗材');
        return;
    }
    
    const newStock = prompt('请输入新的库存数量：');
    if (newStock && !isNaN(newStock) && parseInt(newStock) >= 0) {
        const form = document.getElementById('batchActionForm');
        const stockInput = document.createElement('input');
        stockInput.type = 'hidden';
        stockInput.name = 'new_stock';
        stockInput.value = newStock;
        form.appendChild(stockInput);
        form.action = '{% url "assets:consumable_batch_update_stock" %}';
        form.submit();
    }
}

// 监听单个复选框变化，更新全选状态
document.addEventListener('DOMContentLoaded', function() {
    const consumableCheckboxes = document.querySelectorAll('.consumable-checkbox');
    const selectAll = document.getElementById('selectAll');
    const selectAllHeader = document.getElementById('selectAllHeader');
    
    consumableCheckboxes.forEach(checkbox => {
        checkbox.addEventListener('change', function() {
            const checkedCount = document.querySelectorAll('.consumable-checkbox:checked').length;
            const totalCount = consumableCheckboxes.length;
            
            if (selectAll) selectAll.checked = checkedCount === totalCount;
            if (selectAllHeader) selectAllHeader.checked = checkedCount === totalCount;
        });
    });
});
</script>
{% endblock %}